<?php session_start();
    require_once 'functions.php';
    if(isset($_POST['comments']) && $_POST['comments'] != NULL && isset($_POST['paperID']) && isset($_POST['type']))
    {
       $commentorID = $_SESSION['member_id'];
       $text = mysql_safe($_POST['comments']);
       $span = 0;
       $paperID = $_POST['paperID'];
       $verID = get_latest_version($paperID);
        if($_POST['type'] == "insert") {
        // The query inserts the comment into the database
       $result = mysql_query("INSERT into comment (commentor_id,span_id,version_id,text)
                 VALUES ('$commentorID','$span','$verID','$text')")
                 or die(mysql_error());
       echo '1';
        } else if($_POST['type'] == "update") {
            mysql_query("UPDATE comment SET text = '$text' WHERE version_id = $verID AND commentor_id = $commentorID
                    AND span_id = $span") or die(mysql_error());
            echo '2';
        }
    } else {
        echo "0";
    }





?>
